<?php

namespace EM\NewsBundle\Controller;
use EM\NewsBundle\Form\GeoLangRelationType;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
class LanguageRelationController extends Controller
{
    
   
    public function langSelectionAction(){
        $em = $this->getDoctrine()->getManager();
        $geolang = new \EM\NewsBundle\Entity\GeoLangRelation();
        $form = $this->createForm(new GeoLangRelationType(), $geolang);
        $languages=$em->getRepository('EMNewsBundle:Languages')->findAll();
        $geolanglist=$em->getRepository('EMNewsBundle:Geography')->findByLevel(2);
        return $this->render('EMNewsBundle:Geography:LanguageSelection.html.twig'
                                , array(
                                    'form' => $form->createView(),
                                    'languages' => $languages,
                                    'geolanglist' => $geolanglist,
                                    'idform' => 'create'
                )); 
    }
    public function saveSelectionAction(Request $request){
        $em = $this->getDoctrine()->getManager();
        $languagesl = $this->get('request')->request->get('lang');
        $languages=$em->getRepository('EMNewsBundle:Languages')->findAll();
        $geolanglist=$em->getRepository('EMNewsBundle:Geography')->findByLevel(2);
        $updated=0;
        if($languages!="")
        {
                foreach($languagesl as $ln){
              
                $geolang = new \EM\NewsBundle\Entity\GeoLangRelation();
                $form = $this->createForm(new GeoLangRelationType(), $geolang);
                $request = $this->getRequest();
                $form->bind($request);
                $lang=$em->getRepository('EMNewsBundle:Languages')->find($ln);
                $exists=$em->getRepository('EMNewsBundle:GeoLangRelation')->findBy(array('geo'=>$geolang->getGeo(),'lang'=>$lang->getId()));
              //  echo count($exists);
                 $updated=0;
                if(count($exists)==0){
                $geolang->setLang($lang);

                $em->persist($geolang);
                if(!$em->flush()){
                    $updated=1;
                }
                else
                {
                   return $this->render('EMNewsBundle:Geography:langGeoTable.html.twig'
                                    , array(
                                        'geolanglist' => $geolanglist,
                                        'updated' => $updated
                    )); 
                }
                }
                else
                {
                    $updated=2;

                }
            }
         }
          return $this->render('EMNewsBundle:Geography:langGeoTable.html.twig'
                                , array(
                                    'geolanglist' => $geolanglist,
                                    'updated' => $updated
                )); 
         
    }
    
    public function findSelectionAction($id){
         $em = $this->getDoctrine()->getManager();
         $exists=$em->getRepository('EMNewsBundle:GeoLangRelation')->findBy(array('geo'=>$id));
         $languages=$em->getRepository('EMNewsBundle:Languages')->findAll();
        
         //$out="";
         $languages_selected=array();
       
        if(count($exists)>0){
         foreach($exists as $ex){
            array_push($languages_selected,$ex->getLang()->getId());
         }
          $form = $this->createForm(new GeoLangRelationType(), $exists[0]);
           return $this->render('EMNewsBundle:Geography:_selectLang.html.twig'
                                , array(
                                    'form' => $form->createView(),
                                    'idform' => 'update',
                                    'languages_selected' => $languages_selected,
                                    'languages' => $languages,
                                )); 
        }
        else
        {
           $geolang = new \EM\NewsBundle\Entity\GeoLangRelation();
           $the_country=$em->getRepository('EMNewsBundle:Geography')->find($id);
           $geolang->setGeo($the_country);
           $form = $this->createForm(new GeoLangRelationType(), $geolang);
           return $this->render('EMNewsBundle:Geography:_selectLang.html.twig'
                                , array(
                                    'form' => $form->createView(),
                                    'idform' => 'create',
                                    //'id_combo' => $id,
                                    'languages' => $languages,
                                )); 
        }
          
          
    }
    public function updateSelectionAction(Request $request){
        $em = $this->getDoctrine()->getManager();
        
        $geolang = new \EM\NewsBundle\Entity\GeoLangRelation();     
        $form = $this->createForm(new GeoLangRelationType(), $geolang);
        $request = $this->getRequest();
        $form->bind($request);
         $exists=$em->getRepository('EMNewsBundle:GeoLangRelation')->findBy(array('geo'=>$geolang->getGeo()));
         $geolanglist=$em->getRepository('EMNewsBundle:Geography')->findByLevel(2);
         $select_lang = $this->get('request')->request->get('lang');
         $text="";
         if($select_lang!="")
            {
        
             $result=0;
             $result2=0;
             if(count($exists)>0){
                foreach($exists as $exist){
                 $flag=0;
                foreach($select_lang as $selected){
                    if($exist->getLang()->getId()==$selected){
                        $flag++;
                    }

                }
               // $result.=$exist->getLang()->getId().": ".$flag.", ";
                //if flag is equal to 0, it means whe need to delete 
                if($flag==0){
                   $em->remove($exist);
                   if(!$em->flush()){
                      // echo 'delete';
                         $result++;
                   }
                }

                }
                foreach($select_lang as $selected){
                $geolang2 = new \EM\NewsBundle\Entity\GeoLangRelation();
                $flag2=0;
                 foreach($exists as $exist){
                     if($exist->getLang()->getId()==$selected){
                         $flag2++;
                     }

                 }
               //  $result2.=$selected.": ".$flag2.", ";
                 //if flag is equal to 0, it means whe need to add 
                 if($flag2==0){

                    $lang=$em->getRepository('EMNewsBundle:Languages')->find($selected);
                    $geolang2->setGeo($geolang->getGeo());
                    $geolang2->setLang($lang);
                    $em->persist($geolang2);
                    if(!$em->flush()){
                      //  echo "Added";
                         $result2++;
                    }
                   }

                 }
              }
              else
              {
                  //exists==0
              }
             
               return $this->render('EMNewsBundle:Geography:langGeoTable.html.twig'
                                , array(
                                    'geolanglist' => $geolanglist,
                                    'msg' => $result2.' added and '.$result.' deleted'
                )); 
            }       
        
    }
}
